#[derive(Debug)]
struct Person {
    name: String,
    age: u8,
}

//元组结构体
struct Pair(i32,f32);

struct Point {
    x: f32,
    y: f32,
}

//结构体可以作为另一个结构体的字段
#[allow(dead_code)]
struct Rectangle {
    top_left: Point,
    bottom_right: Point,
}

impl Rectangle {
    fn area(&self)-> f32 {
        let Point{x: top_x,y:top_y} = self.top_left;
        let Point{x:bottom_x,y:bottom_y} = self.bottom_right;
        (bottom_x-top_x) * (bottom_y-top_y)
    }
}
pub fn example() {
    let name = String::from("Peter");
    let age = 28;
    let peter = Person {name,age};

    println!("{:?}",peter);
    let point: Point = Point {x: 10.3,y: 0.4};
    println!("point coordinates: ({},{})",point.x,point.y);
    let bottom_right = Point {x: 5.2,..point};

    println!("second point: ({}, {})",bottom_right.x,bottom_right.y);

    let Point {x: left_edge,y: top_edge} = point;

    let _rectangle = Rectangle{
        top_left: Point{x: left_edge,y: top_edge},
        bottom_right: bottom_right,
    };

    // 实例化一个元组结构体
    let pair = Pair(1, 0.1);

    // 访问元组结构体的字段
    println!("pair contains {} and {}", pair.0, pair.1);

    // 解构一个元组结构体
    let Pair(integer, decimal) = pair;

    println!("pair contains {:?} and {:?}", integer, decimal);
}


pub fn practise1() {
    let mut rect = Rectangle {
        top_left: Point{x:1.0,y:3.0},
        bottom_right: Point{x:5.0,y:10.0},
    };
    println!("{}",rect.area());
}